package mall

import (
	"context"
	"github.com/zeromicro/go-zero/core/logx"
	"private-domain-overseas-service/app/business/api/internal/svc"
	"private-domain-overseas-service/tools/ent"
	"private-domain-overseas-service/tools/ent/hzmall"
	"private-domain-overseas-service/tools/ent/predicate"
)

type MallDao struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewMallDao(ctx context.Context, svcCtx *svc.ServiceContext) *MallDao {
	return &MallDao{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

// MallConditionsAll - 根据条件查询多条数据
func (l *MallDao) MallConditionsAll(conditions []predicate.HzMall) ([]*ent.HzMall, error) {
	malls, err := l.svcCtx.Orm.HzMall.Query().
		Where(conditions...).
		Order(ent.Desc(hzmall.FieldMallSort)).
		All(l.ctx)
	if err != nil {
		return nil, err
	}
	return malls, nil
}

// MallConditionsFirst -根据条件查询单个商城图标
func (l *MallDao) MallConditionsFirst(conditions []predicate.HzMall) (*ent.HzMall, error) {
	hzMall, err := l.svcCtx.Orm.HzMall.Query().Where(conditions...).First(l.ctx)
	if err != nil {
		return nil, err
	}
	return hzMall, nil

}
